<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/9 0009
 * Time: 11:13
 */

namespace app\currency\service;


use think\Db;

class AgentService
{
    /**
     * 获取所有团队的ID列表
     * @param $aid
     * @return array|mixed
     */
    public static function getTeamIds($aid)
    {
        $cleaner = Db::name('Cleaner')->where([['agent_id','eq',$aid]])->column('id');
        if(empty($cleaner))
        {
            return [];
        }
        $pids = implode(',',$cleaner);
        $sql = "SELECT id,`status`,ischild FROM (
SELECT T1.id,T1.`status`,if(FIND_IN_SET(pid,@pids) > 0,@pids := CONCAT(@pids,',',id),0) as ischild
 FROM (SELECT id,pid,`status` FROM cleaner T WHERE 1 = 1) T1,(SELECT @pids := '{$pids}') T2
 ) T3 WHERE ischild != 0 AND `status` != 0";
        $result = Db::query($sql);
        if($result)
        {
            $result = array_merge(array_unique(array_column($result,'id')),$cleaner);
            sort($result);
            return $result;
        } else {
            return $cleaner;
        }
    }
}